package com.flash.DayProcessing.service.impl;

import com.flash.DayProcessing.entity.IncomePayments;
import com.flash.DayProcessing.mapper.IncomePaymentsMapper;
import com.flash.DayProcessing.service.IncomePaymentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class IncomePaymentsServiceImpl implements IncomePaymentsService{
    @Resource
    IncomePaymentsMapper incomePaymentsMapper;


    @Override
    public List<IncomePayments> selectCashInterestIncome(int page, int limit, String statDate, String fundId) {
        HashMap map = new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //创建一个 String类型的空时间
        String datetime="";
        //创建Date类对象
        Date date=new Date();
        //判断传入的statDate是否为null/空
        if(statDate!=null&& !statDate.equals("")){
            try {
                //减一天 86400*1000
                long dif= df.parse(statDate).getTime() - 86400 * 1000;
                //把 dif 传给Date
                date.setTime(dif);
                //将使用format将date转换成日期类格式
                datetime=df.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            //new SimpleDateFormat对象
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            //创建Calendar 的实例
            Calendar calendar = Calendar.getInstance();
            //当前时间减去一天，即一天前的时间
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            //获取当天时间
            statDate=simpleDateFormat.format(date);
            //获取当天-1的时间 calendar.getTime()
            datetime=simpleDateFormat.format(calendar.getTime());
        }
        System.out.println("statDate="+statDate);
        System.out.println("日期"+datetime);
        map.put("ccc",buildWhere(statDate,fundId));
        return incomePaymentsMapper.selectCashInterestIncome(map);
    }
    public String buildWhere(String statDate,String fundId){
        System.out.println("这是条件日期查询");
        StringBuffer strWhere=new StringBuffer();
        if(statDate!=null && !statDate.equals("")){
            strWhere.append(" and c.dateAccrualStock = '" + statDate + "'" );
            strWhere.append(" and c.remark = '" + fundId+ "'");
        }
        System.out.println(strWhere.toString());
        return strWhere.toString();
    }

    public String buildWheres(String statDate,String fundId){
        StringBuffer strWhere=new StringBuffer();
        if(statDate!=null && !statDate.equals("")){
            strWhere.append(" and s.dateAccrualStock = '" + statDate + "'" );
            strWhere.append(" and s.fundId = '" + fundId+ "'");
        }
        return strWhere.toString();
    }
    @Override
    public List<IncomePayments> selectBondInterestIncome(int page, int limit, String statDate, String fundId) {
        HashMap map = new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //创建一个 String类型的空时间
        String datetime="";
        //创建Date类对象
        Date date=new Date();
        //判断传入的statDate是否为null/空
        if(statDate!=null&& !statDate.equals("")){
            try {
                //减一天 86400*1000
                long dif= df.parse(statDate).getTime() - 86400 * 1000;
                //把 dif 传给Date
                date.setTime(dif);
                //将使用format将date转换成日期类格式
                datetime=df.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            //new SimpleDateFormat对象
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            //创建Calendar 的实例
            Calendar calendar = Calendar.getInstance();
            //当前时间减去一天，即一天前的时间
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            //获取当天时间
            statDate=simpleDateFormat.format(date);
            //获取当天-1的时间 calendar.getTime()
            datetime=simpleDateFormat.format(calendar.getTime());
        }
        System.out.println("statDate="+statDate);
        System.out.println("日期"+datetime);
        map.put("bbb",buildWheres(datetime,fundId));
        return incomePaymentsMapper.selectBondInterestIncome(map);
    }

    @Override
    public List<IncomePayments> selectPayTwoFees(int page, int limit, String statDate, String fundId) {
        HashMap map = new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //创建一个 String类型的空时间
        String datetime="";
        //创建Date类对象
        Date date=new Date();
        //判断传入的statDate是否为null/空
        if(statDate!=null&& !statDate.equals("")){
            try {
                //减一天 86400*1000
                long dif= df.parse(statDate).getTime() - 86400 * 1000;
                //把 dif 传给Date
                date.setTime(dif);
                //将使用format将date转换成日期类格式
                datetime=df.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            //new SimpleDateFormat对象
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            //创建Calendar 的实例
            Calendar calendar = Calendar.getInstance();
            //当前时间减去一天，即一天前的时间
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            //获取当天时间
            statDate=simpleDateFormat.format(date);
            //获取当天-1的时间 calendar.getTime()
            datetime=simpleDateFormat.format(calendar.getTime());
        }
        System.out.println("statDate="+statDate);
        System.out.println("日期"+datetime);
        map.put("aaa",buildWhere(datetime,fundId));
        return incomePaymentsMapper.selectPayTwoFees(map);
    }

}
